import modelExtend from 'dva-model-extend'
import api from 'api'
import { pathMatchRegexp } from 'utils'
import { pageModel } from 'utils/model'
import { message } from 'antd'

const { getuserauths, setuserauth, setauths } = api

export default modelExtend(pageModel, {
  namespace: 'userSetAuth',

  state: {
    data: {},
    select: '',
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        let { pathname } = location
        const match = pathMatchRegexp('/auths/userauth/:id', pathname)
        if (match) {
          // console.log("userid:" + JSON.stringify(match));
          dispatch({
            type: 'updateState',
            payload: {
              select: match[1],
            },
          })
          dispatch({ type: 'query', payload: { userid: match[1] } })
        }
      })
    },
  },

  effects: {
    *query({ payload }, { call, put }) {
      const { result } = yield call(getuserauths, payload)
      yield put({
        type: 'updateState',
        payload: {
          data: result,
        },
      })
    },
    *setuserauth({ payload }, { call }) {
      message.loading('修改中...', 0)
      yield call(setuserauth, payload)
      message.destroy()
      message.success('修改成功')
    },
    *getallauths({ payload }, { call, put, select }) {
      const data = yield call(setauths, payload)
    },
  },

  reducers: {},
})
